<?php

class AdminStoryController  extends BaseController{
	
	/**
	 * The layout that should be used for responses.
	 */
	protected $layout = 'layouts.admin';
	
	public function showStories(){
		$stories = Story::orderBy("id","desc")->paginate(50);//
		// 		$products = Products::withTrashed()->orderBy('id','desc')->forPage(50,2)->get();
		$content = View::make('admin.story.stories');
		$this->layout->content = $content;
		$content->with("stories",$stories);
	}
	
	
	public function showStoryAdd (){
		
		$id = Input::get("id",0);
		$content = View::make('admin.story.story-add');
		$this->layout->content = $content;
		if (!empty($id)){
			$story = Story::find($id);
			$content->with("story",$story);
		}
	}
	
	public function postStory() {
		$s = Input::all();
		$id = Input::get('id',0);
		
		$image = Input::file("image");
		if (empty($id)){
			$image = Images::storeImage($image);
			$s["image"] = $image;
			$story = Story::create($s);
		} else {
			//如果存在则更新
			$story = Story::find($id);
			if (Input::hasFile("image")){
				$image = Images::storeImage($image,$story->image);
				$s["image"] = $image;
			} else {
				//$p["image"] = null;
				$s = array_except($s,"image");
			}
			$story->update($s);
		}
		return Redirect::route('story');
	}
	
	public function deleteStory(){
		$id = Input::get('id',0);
		Story::destroy($id);
		return Response::json(array('success'=>true,'id'=>$id));
	}
}

?>